import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.JDBCUtils.JDBCUtils;
import com.jspsmart.upload.SmartUpload;


public class banner extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public banner() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //�ƻ����� �����ݿ��е�banner�����洢���е�����,Ϊ���Ժ�������ݵ�ʱ�򷽱�.����������������д���ڳ�������
        //��������ʵ�ֻ���д�����˳�������.�Ժ�ð�.
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        String sqlGetBanner = "SELECT * FROM `test`.`all_goods` ORDER BY `Commission_jihua` DESC LIMIT 3;";
        JDBCUtils jdbcUtils = new JDBCUtils();
        List<Map<String, Object>> getBannerResultList = null;
        try {
            getBannerResultList = jdbcUtils.query(sqlGetBanner, null);
        } catch (SQLException e) {
            out.print("-1");
            out.close();
            jdbcUtils.close();
            e.printStackTrace();
        }


        JSONObject resultObject = new JSONObject();
        JSONArray dataJsonArray = new JSONArray();

        for (int i = 0; i < getBannerResultList.size(); i++) {
            JSONObject tempJsonObject = new JSONObject();
            tempJsonObject.put("type", "good");
            tempJsonObject.put("goodsInfo", getBannerResultList.get(i));

            dataJsonArray.add(tempJsonObject);
        }
        resultObject.put("result", dataJsonArray);

        out.print(resultObject);

        jdbcUtils.close();

        out.flush();
        out.close();

    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
